/-docs
/-editor
/-files
/-files-old
/-imports
/-layout
/-storage ...
/-storage/attached ...
/-storage/attached/api
/-storage/attached/dom ...
DetectStorage.ts
LoadStorage.ts
UpdateStorage.ts
/-storage/attached/indexedDB
/-storage/attached/localStorage
/-storage/attached/webSQL
/-tests
/-tests/files
/-tests/storage
TestCase.html
TestCase.ts
TestPage.css
TestPage.html
TestPage.ts
_sampleTests.ts
teapo-tests.html
teapo-tests.ts
/-typings
TypeScriptService.ts
functions.ts
ko.ts
persistence.api.ts
persistence.ts
shell.ts
teapo.html
teapo.ts
x
 
1
module teapo.storage.attached.dom {
2
3
  export class UpdateStorage {
4
5
    constructor(
6
      private _parentElement: HTMLElement,
7
      private _byName: { [fullPath: string]: HTMLElement; },
8
      private _document: { createElement(tag: string): HTMLElement; }) {
9
    }
10
11
    update(file: string, property: string, value: string, callback?: (error: Error) => void): void {
12
13
      var element = this._getExistingElement(file);
14
15
      if (!element) {
16
        element = UpdateStorage.createElement(this._parentElement, file, this._document);
17
        this._byName[file] = element;
18
      }
19
20
      UpdateStorage.updateProperty(element, property, value);
21
22
      this._parentElement.setAttribute('data-edited-utc', Date.now() + '');
23
24
      callback(null);
25
    }
26
27
    remove(file: string, callback?: (error: Error) => void): void {
28
29
      var element = this._getExistingElement(file);
30
      if (!element) {
31
        callback(new Error('file does not exist.'));
32
        return;
33
      }
34
35
      element.parentElement.removeChild(element);
36
      delete this._byName[file];
37
38
      this._parentElement.setAttribute('data-edited-utc', Date.now() + '');
39
40
      callback(null);
41
    }
42
43
    static createElement(parentElement: HTMLElement, fullPath: string, _document: { createElement(tag: string): HTMLElement; }): HTMLElement {
44
      var element = _document.createElement('div');
45
      element.setAttribute('data-path', fullPath);
46
      parentElement.appendChild(element);
47
      return element;
48
    }
49
50
    static updateProperty(element: HTMLElement, property: string, value: string): void {
51
      element.setAttribute('data-meta-' + encodeForAttributeName(property), value);
52
    }
53
54
    private _getExistingElement(fullPath): HTMLElement {
55
      var element = this._byName.hasOwnProperty(fullPath) ? this._byName[fullPath] : null;
56
      return element;
57
    }
58
59
  }
60
61
}
43:48 primitive type string createElement(tag: string): HTMLElement